<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="主叫爱洞ID" prop="callPlatformId">
        <el-input
          v-model="queryParams.callPlatformId"
          placeholder="请输入主叫爱洞ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="被叫爱洞ID" prop="answerPlatformId">
        <el-input
          v-model="queryParams.answerPlatformId"
          placeholder="请输入被叫爱洞ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="话单编号" prop="ucid">
        <el-input
          v-model="queryParams.ucid"
          placeholder="请输入通话唯一标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.call_check_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handlePass"
          v-hasPermi="['system:callVoiceCheck:pass']"
        >一键审核忽略
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          :disabled="multiple"
          @click="handleRefuse"
          v-hasPermi="['system:callVoiceCheck:refuse']"
        >一键审核违规
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="callVoiceCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主叫爱洞ID" align="center" prop="callPlatformId">
        <template slot-scope="scope">
          <a v-if="scope.row.callSex === 1" style="color: #FF6EC7" @click="toUserInfo(scope.row.callPlatformId)">{{ scope.row.callPlatformId }}
          </a>
          <a v-else style="color: #7099F9" @click="toUserInfo(scope.row.callPlatformId)">{{ scope.row.callPlatformId }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="被叫爱洞ID" align="center" prop="answerPlatformId">
        <template slot-scope="scope">
          <a v-if="scope.row.answerSex === 1" style="color: #FF6EC7" @click="toUserInfo(scope.row.answerPlatformId)">{{ scope.row.answerPlatformId }}
          </a>
          <a v-else style="color: #7099F9" @click="toUserInfo(scope.row.answerPlatformId)">{{ scope.row.answerPlatformId }}
          </a>
        </template>
      </el-table-column>
      <el-table-column label="话单编号" align="center" prop="ucid"/>

      <el-table-column label="命中关键词" align="center" prop="keyWord">
        <template slot-scope="scope">
          <el-tag v-for="(key,index) in scope.row.keyWords" :key="index" type="danger">{{ key.resultText }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.call_check_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="违规录音" align="center" prop="voiceUrl">
        <template slot-scope="scope">
          <VoicePreview :src="scope.row.voiceUrl"></VoicePreview>
        </template>
      </el-table-column>
      <el-table-column label="识别文本" align="center" prop="voiceText"/>
      <el-table-column label="审核人" align="center" prop="updateBy"/>
      <el-table-column label="审核时间" align="center" prop="updateTime"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.status!=1" size="mini" type="text" icon="el-icon-check"
                     @click="handlePass(scope.row)" v-hasPermi="['system:callVoiceCheck:pass']">审核忽略
          </el-button>
          <el-button :disabled="scope.row.status!=1" size="mini" type="text" icon="el-icon-close"
                     @click="handleRefuse(scope.row)" v-hasPermi="['system:callVoiceCheck:refuse']">判定违规
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 通过审核 -->
    <el-dialog title="通过审核" :visible.sync="openPass" width="500px" append-to-body>
      <el-form ref="form" :model="bo" :rules="rules" label-width="80px">
        <el-form-item label="操作备注" prop="reason">
          <el-input v-model="bo.reason" placeholder="请输入操作备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toPass">确 定</el-button>
        <el-button @click="openPass=false">取 消</el-button>
      </div>
    </el-dialog>


    <!--拒绝审核 -->
    <el-dialog title="拒绝审核" :visible.sync="openRefuse" width="500px" append-to-body>
      <el-form ref="form" :model="bo" :rules="rules" label-width="80px">
        <el-form-item label="操作备注" prop="reason">
          <el-input v-model="bo.reason" placeholder="请输入操作备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toRefuse">确 定</el-button>
        <el-button @click="openRefuse=false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  addCallVoiceCheck,
  delCallVoiceCheck,
  getCallVoiceCheck,
  listCallVoiceCheck,
  pass,
  refuse,
  updateCallVoiceCheck
} from "@/api/service/call/callVoiceCheck";

export default {
  name: "CallVoiceCheck",
  dicts: ['call_check_status'],
  data() {
    return {
      bo: {},
      openRefuse: false,
      openPass: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 通话录音检查表格数据
      callVoiceCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        callPlatformId: undefined,
        answerPlatformId: undefined,
        ucid: undefined,
        keyWord: undefined,
        status: '1',
        voiceUrl: undefined,
        voiceText: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          {required: true, message: "不能为空", trigger: "blur"}
        ],
        callPlatformId: [
          {required: true, message: "主叫爱洞ID不能为空", trigger: "blur"}
        ],
        answerPlatformId: [
          {required: true, message: "被叫爱洞ID不能为空", trigger: "blur"}
        ],
        ucid: [
          {required: true, message: "通话唯一标识不能为空", trigger: "blur"}
        ],
        keyWord: [
          {required: true, message: "命中关键词不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "1=未处理 2=忽略 3=违规不能为空", trigger: "change"}
        ],
        voiceUrl: [
          {required: true, message: "违规录音不能为空", trigger: "blur"}
        ],
        remark: [
          {required: true, message: "备注不能为空", trigger: "blur"}
        ],
        voiceText: [
          {required: true, message: "录音识别文本不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 审核通过 */
    handlePass(row) {
      console.log(row)
      if (row.id) {
        this.bo.id = [row.id]
      } else {
        this.bo.id = this.ids
      }
      this.openPass = true;
    },

    /** 删除按钮操作 */
    handleRefuse(row) {
      console.log(row)
      if (row.id) {
        this.bo.id = [row.id]
      } else {
        this.bo.id = this.ids
      }
      this.openRefuse = true;
    },
    toPass() {
      pass(this.bo).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openPass = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    toRefuse() {
      refuse(this.bo).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.openRefuse = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 查询通话录音检查列表 */
    getList() {
      this.loading = true;
      listCallVoiceCheck(this.queryParams).then(response => {
        this.callVoiceCheckList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        callPlatformId: undefined,
        answerPlatformId: undefined,
        ucid: undefined,
        keyWord: undefined,
        status: undefined,
        voiceUrl: undefined,
        remark: undefined,
        voiceText: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加通话录音检查";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getCallVoiceCheck(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改通话录音检查";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCallVoiceCheck(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addCallVoiceCheck(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除通话录音检查编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delCallVoiceCheck(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/callVoiceCheck/export', {
        ...this.queryParams
      }, `callVoiceCheck_${new Date().getTime()}.xlsx`)
    },
    /**
     * 点击用户爱动id跳转到用户页面
     * @param platformId
     */
    toUserInfo(platformId) {
      this.$router.push({path: "/user/appUser", query: {id: platformId}});
    }
  }
};
</script>
